package org.darkforest.common.services.impl;

import org.darkforest.common.services.VoucherTemplateService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.VoucherMapper;
import org.darkforest.dao.mapper.VoucherTplMapper;
import org.darkforest.model.Voucher;
import org.darkforest.model.VoucherTpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/12/30.
 */
@Service
public class VoucherTemplateServiceImpl implements VoucherTemplateService {

    @Autowired
    private VoucherTplMapper voucherTplMapper;

    @Autowired
    private VoucherMapper voucherMapper;

    @Override
    public int deleteByPrimaryKey(Integer voucherTId) {
        return voucherTplMapper.deleteByPrimaryKey(voucherTId);
    }

    @Override
    public int insert(VoucherTpl record) {
        return voucherTplMapper.insert(record);
    }

    @Override
    public int insertSelective(VoucherTpl record) {
        return voucherTplMapper.insertSelective(record);
    }

    @Override
    public VoucherTpl selectByPrimaryKey(Integer voucherTId) {
        return voucherTplMapper.selectByPrimaryKey(voucherTId);
    }

    @Override
    public int updateByPrimaryKeySelective(VoucherTpl record) {
        return voucherTplMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(VoucherTpl record) {
        return voucherTplMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据条件分页查询代金券
     *
     * @param voucherTpl
     * @return
     */
    @Override
    public VoucherTpl selectVoucherTplListByPage(VoucherTpl voucherTpl) {
        voucherTpl.setResultList(voucherTplMapper.selectVoucherTplList(voucherTpl));
        return  voucherTpl;
    }

    /****
     * 定时修改过期代金券的数据状态,取消未使用的代金券模板，取消未使用的代金券
     */
    public void voucherTask(){
        VoucherTpl voucherTpl = new VoucherTpl();
        voucherTpl.setVoucherTEndDate(DateUtil.toUnixTimeStampByDate(DateUtil.toTimeString(new Date())));
        voucherTpl.setVoucherTState((byte)1);
        List<VoucherTpl>  voucherTpls = voucherTplMapper.selectOverTimeVoucherTplList(voucherTpl);
        for(VoucherTpl vtp:voucherTpls){
            vtp.setVoucherTState((byte)2);
            voucherTplMapper.updateByPrimaryKeySelective(vtp);
            Voucher record = new Voucher();
            record.setVoucherTId(vtp.getVoucherTId());
            voucherMapper.updateOverTimeVoucher(record);
        }

    }

    /**
     * 更新模板中已经使用的优惠券数量
     *
     * @param voucherTId
     */
    @Override
    public void updateVoucherTplUserNumber(Integer voucherTId) {
        voucherTplMapper.updateVoucherTplUserNumber(voucherTId);
    }


    public List<VoucherTpl> selectVoucherTplList(VoucherTpl voucherTpl){
        return voucherTplMapper.selectVoucherTplList(voucherTpl);
    }

    public List<VoucherTpl> selectVoucherTplListAndVoucherNum(VoucherTpl voucherTpl){
        return voucherTplMapper.selectVoucherTplListAndVoucherNum(voucherTpl);
    }
}
